package com.dmatek.uwb.local.algorithm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.dmatek.uwb.packet.bean.Weight;
import com.dmatek.uwb.util.tool.StringUtil;
/***
 * 权重具体实现工具
 * @author zhangfu
 * @data 2019年6月22日 下午2:49:09
 * @Description
 */
public class WeightTool implements IWeightingTool {
	private Map<String, Weight> weightMap = null;
	public WeightTool() {
		this.weightMap = new HashMap<String, Weight>();
	}
	@Override
	public void init(List<String> weightIds) {
		this.weightMap.clear();
		for (String id : weightIds) {
			this.weightMap.put(id, new Weight(id));
		}
	}
	@Override
	public void increase(String id) {
		Weight weight = weightMap.get(id);
		if(null != weight) { 
			// 设置权重值
			int val = weight.getVal() + 1;
			weight.setVal(val);
		}
	}
	@Override
	public List<Weight> list() {
		// 浏览权重列表
		return new ArrayList<Weight>(weightMap.values());
	}
	@Override
	public Weight obtainMaxWeight() {
		int maxVal = -1;
        String maxId = "";
		for (Weight weight : weightMap.values()) {
			if (maxVal < weight.getVal()) {
                maxId = weight.getId();
                maxVal = weight.getVal();
            } else if (maxVal == weight.getVal()) { 
            	/**
            	 * 说明此时存在两个权重值相等的情况，此时两个值都不应该取，
            	 * 只能从后面的值的取
            	 * */ 
                maxId = "";
            }
		}
		if(!StringUtil.isEmpty(maxId)) {
			return weightMap.get(maxId);
		}
		return null;
	}
	public Map<String, Weight> getWeightMap() {
		return weightMap;
	}
	public void setWeightMap(Map<String, Weight> weightMap) {
		this.weightMap = weightMap;
	}
}
